package com.k.db.dameng;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class DamengDemo {
    private static Connection getConnection(String driverClassName,String url,String username, String password) throws ClassNotFoundException, SQLException {
        Class.forName(driverClassName);
        return DriverManager.getConnection(url, username, password);
    }
    private static Connection getConnectionDameng(String url,String username,String password) throws SQLException, ClassNotFoundException {
        String driverClassName = "dm.jdbc.driver.DmDriver";
        return getConnection(driverClassName,url,username,password);
    }
    private static Connection getConnectionPostgres(String url,String username,String password) throws SQLException, ClassNotFoundException {
        String driverClassName = "org.postgresql.Driver";
        return getConnection(driverClassName,url,username,password);
    }
    private static String getDatabaseProductName(Connection conn) throws SQLException {
        DatabaseMetaData metaData = (DatabaseMetaData) conn.getMetaData();
        String productName = metaData.getDatabaseProductName();
        System.out.println("数据库的产品名称:" + productName);
        return productName;
    }
    private static void closeResource(Connection conn,Statement st,PreparedStatement pst,ResultSet rs){
        if (st != null) {
            try {
                st.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (pst != null) {
            try {
                pst.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (conn != null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    private static void testDameng(){
        // Localhost
        String driverClassName = "dm.jdbc.driver.DmDriver";
        String url = "jdbc:dm://10.110.12.33:5237/testljw1";
        String username = "SYSDBA";
        String password = "SYSDBA";

        url = "jdbc:dm://192.168.240.84:5236/ads";

        Connection conn = null;
        Statement st = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from \"user_info\"";
        sql = "SELECT CASE_SENSITIVE()";
        sql = "select * from person_info ";
        sql = "select * from duty_guard_model";

        try {
            conn = getConnectionDameng(url,username,password);
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while (rs.next()) {
                String str1 = rs.getString(1);
                String str2 = rs.getString(2);
                System.out.println("str1:"+str1 +"|"+str2);
            }

            String pname = getDatabaseProductName(conn);
            System.out.println(pname);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(conn,st,pst,rs);
        }
    }
    public static void testPg(){
        String driverClassName = "org.postgresql.Driver";
        String url = "jdbc:postgresql://192.168.241.89:5432/ads";
        String username = "postgres";
        String password = "Command_123";
        Connection conn = null;
        Statement st = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String sql = "select * from \"user_info\"";
        try {
//            Class.forName(driverClassName);
//            conn = DriverManager.getConnection(url, username, password);
//            pst = conn.prepareStatement(sql);
//            rs = pst.executeQuery();
//            while (rs.next()) {
//                String str1 = rs.getString(1);
//                String str2 = rs.getString(2);
//                System.out.println("str1:"+str1 +"|"+str2);
//            }
            conn = getConnectionPostgres(url,username,password);
            String pname = getDatabaseProductName(conn);
            System.out.println(pname);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(conn,st,pst,rs);
        }
    }

    public static void main(String[] args) {
        testDameng();
//        testPg();
    }
}
